package com.sean.item;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AbsSeekBar;

import com.sean.util.UnitUtil;

/**
 * 垂直拖动条
 * @author Sean
 * 
 */
public class VerticalSeekBar extends AbsSeekBar {

	private Drawable mThumb;
	private Drawable mProgressDrawable;
	
	private int mHeight;
	private int mWidth;
	
	private float mScale;
	
	private int mProcessWidth = 6;
	
	private int mInnerPadding;
	
	public interface OnSeekBarChangeListener {
		void onProgressChanged(VerticalSeekBar VerticalSeekBar, int progress,
				boolean fromUser);

		void onStartTrackingTouch(VerticalSeekBar VerticalSeekBar);

		void onStopTrackingTouch(VerticalSeekBar VerticalSeekBar);
	}

	private OnSeekBarChangeListener mOnSeekBarChangeListener;

	public VerticalSeekBar(Context context) {
		this(context, null);
	}

	public VerticalSeekBar(Context context, AttributeSet attrs) {
		this(context, attrs, android.R.attr.seekBarStyle);
	}

	public VerticalSeekBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public void setOnSeekBarChangeListener(OnSeekBarChangeListener l) {
		mOnSeekBarChangeListener = l;
	}

	void onStartTrackingTouch() {
		if (mOnSeekBarChangeListener != null) {
			mOnSeekBarChangeListener.onStartTrackingTouch(this);
		}
	}

	void onStopTrackingTouch() {
		if (mOnSeekBarChangeListener != null) {
			mOnSeekBarChangeListener.onStopTrackingTouch(this);
		}
	}
	
	public int getProcessWidth() {
		return (int)UnitUtil.dip2px(this.getContext(), mProcessWidth);
	}

	/**
	 * 该方法为重写方法
	 */
	void onProgressRefresh(float scale, boolean fromUser) {
		if(scale > 1) {
			scale = 1.0f;
		}
		mScale = scale;
		invalidate();
		if (mOnSeekBarChangeListener != null) {
			mOnSeekBarChangeListener.onProgressChanged(this, getProgress(),
					fromUser);
		}
	}
	
	@Override
	public void setProgressDrawable(Drawable d) {
		this.mProgressDrawable = d;
		super.setProgressDrawable(d);
	}

	/**
	 * 将画布旋转90度，既可实现垂直拖动条的效果
	 */
	protected synchronized void onDraw(Canvas c) {
	
		c.rotate(-90);
		c.translate(-mHeight, 0);
		
		//把拖动条和拖动按钮调到居中位置
		calculateBounds();
		mProgressDrawable.draw(c);
		mThumb.draw(c);
	}
	
	/**
	 * 计算拖动条和拖动按钮的画图区域
	 */
	private void calculateBounds() {
		
		//设置拖动按钮的画图区域
		int thumbWidth = mThumb.getIntrinsicWidth();
		int available = mHeight - getPaddingTop() - getPaddingBottom() - mInnerPadding * 2;
		int thumbPos = (int) (mScale * available) + getPaddingBottom();
		
		Rect thumbRect = new Rect();
		thumbRect.left = thumbPos;
		thumbRect.right = thumbPos + thumbWidth;
		thumbRect.top = mWidth / 2 - mInnerPadding;
		thumbRect.bottom = thumbRect.top + thumbWidth;
//		System.out.println("thumbRect.left:" + thumbRect.left + "; thumbRect.right:" + thumbRect.right);
		if(thumbRect.right > mHeight) {
			thumbRect.right = mHeight;
			thumbRect.left = thumbRect.right - thumbWidth;
		}
		mThumb.setBounds(thumbRect);
		
		//设置拖动条的画图区域
		Rect processRect = new Rect();
		processRect.left = (int)UnitUtil.dip2px(this.getContext(), 3);
		processRect.right = this.mHeight - (int)UnitUtil.dip2px(this.getContext(), 3);
		processRect.top = mWidth / 2 - getProcessWidth() / 2;
		processRect.bottom = processRect.top + getProcessWidth();
		mProgressDrawable.setBounds(processRect);
	}

	protected synchronized void onMeasure(int widthMeasureSpec,
			int heightMeasureSpec) {
		mHeight = View.MeasureSpec.getSize(heightMeasureSpec);
		mWidth = View.MeasureSpec.getSize(widthMeasureSpec);
		this.setMeasuredDimension(mWidth, mHeight);
	}

	@Override
	public void setThumb(Drawable thumb) {
		mThumb = thumb;
		super.setThumb(thumb);
		mInnerPadding = mThumb.getIntrinsicWidth() / 2;
		
	}
	
	

	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(h, w, oldw, oldh);
	}

	public boolean onTouchEvent(MotionEvent event) {
		if (!isEnabled()) {
			return false;
		}
		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			setPressed(true);
			onStartTrackingTouch();
			trackTouchEvent(event);
			break;

		case MotionEvent.ACTION_MOVE:
			trackTouchEvent(event);
			attemptClaimDrag();
			break;

		case MotionEvent.ACTION_UP:
			trackTouchEvent(event);
			onStopTrackingTouch();
			setPressed(false);
			break;

		case MotionEvent.ACTION_CANCEL:
			onStopTrackingTouch();
			setPressed(false);
			break;
		}
		return true;
	}
	
	private void trackTouchEvent(MotionEvent event) {
		final int height = getHeight();
		final int available = height - getPaddingBottom() - getPaddingTop() - mInnerPadding * 2;
		int y = (int) event.getY();
		float progress = 0;
		if (y > height - getPaddingBottom() - mInnerPadding) {
			mScale = 0.0f;
		} else if (y < getPaddingTop() - mInnerPadding) {
			mScale = 1.0f;
		} else {
			mScale = (float) (height - getPaddingBottom() - mInnerPadding - y)
					/ (float) available;
		}

		final int max = getMax();
		progress = mScale * max;
		setProgress((int) progress);
	}

	private void attemptClaimDrag() {
		if (getParent() != null) {
			getParent().requestDisallowInterceptTouchEvent(true);
		}
	}

	/**
	 * 派发键盘事件，实现链接按钮的控制
	 */
	public boolean dispatchKeyEvent(KeyEvent event) {
		if (event.getAction() == KeyEvent.ACTION_DOWN) {
			KeyEvent newEvent = null;
			switch (event.getKeyCode()) {
			case KeyEvent.KEYCODE_DPAD_UP:
				newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,
						KeyEvent.KEYCODE_DPAD_RIGHT);
				break;
			case KeyEvent.KEYCODE_DPAD_DOWN:
				newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,
						KeyEvent.KEYCODE_DPAD_LEFT);
				break;
			case KeyEvent.KEYCODE_DPAD_LEFT:
				newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,
						KeyEvent.KEYCODE_DPAD_DOWN);
				break;
			case KeyEvent.KEYCODE_DPAD_RIGHT:
				newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,
						KeyEvent.KEYCODE_DPAD_UP);
				break;
			default:
				newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,
						event.getKeyCode());
				break;
			}
			return newEvent.dispatch(this);
		}
		return false;
	}

	/**
	 * 由于画布旋转了90度，所以上下左右的PADDING数值要进行调整
	 */
	@Override
	public void setPadding(int left, int top, int right, int bottom) {
		super.setPadding(bottom, left, top, right);
	}

	@Override
	public int getPaddingTop() {
		return super.getPaddingLeft();
	}

	@Override
	public int getPaddingBottom() {
		return super.getPaddingRight();
	}

	@Override
	public int getPaddingLeft() {
		return super.getPaddingBottom();
	}

	@Override
	public int getPaddingRight() {
		return super.getPaddingTop();
	}
}
